<?php

namespace app\common\model;

use think\Db;

/**
 *   智能推送
 */
class Intelligent extends Base{
	//TYPE=1:必须传UID
    /**
      * articles [desc]
      *
      * @param type $type    [消息类型 1：定制书籍推荐 2：猜你喜欢推荐 3：关键词回复]
      * @param type $uid     [用户ID]
      * @param type $cps_uid [平台用户ID]
      * @param type $appid   [平台微信公众号appid]
	  * @param type $appid   [平台微信wxmpid]
      *
      * @return type
    **/
	public function articles($type,$uid,$cps_uid,$appid,$wxmpid=0) {

		

		$data = array();
		$num=4;
		$cover="/uploads/history.png";//720*400封面图
		$site_url = get_menuurl($cps_uid,0,$appid);
		switch($type){
			//定制书籍推荐
			case '1':
				$history = db('history')->where(array("model"=>1,"uid"=>$uid))->order("update_time DESC")->find();
				
			    if($history){
					$data["articles"][] = array(
						"title"=>"继续阅读：《".$history["name"]."》",
						"picurl"=>$site_url.$cover,
						"url"=>$site_url."/chapter/read/".$history["articleid"]."/".$history["chapterid"],
					);
			    }else{
					$list  = $this->random_data(1,"novel",array("status"=>1,"allvisit"=>array(">=",100)),"id,title,cover");
				
					if($list){

						foreach ($list as $k=>$v){
							$data["articles"][] = array(
								"title"=>$v["title"],
								"picurl"=>$site_url.$cover,
								"url"=>$site_url."/book/info/id/".$v["id"],
							);
						}

					}
				}
				
				break;
			case '2'://猜你喜欢推荐
				
				$list  = $this->random_data($num,"novel",array("status"=>1,"allvisit"=>array(">=",100)),"id,title,cover");

				if($list){

					foreach ($list as $k=>$v){
						$noveldata[] = array(
							"title"=>$v["title"],
							"picurl"=>$site_url.$v["cover"],
							"url"=>$site_url."/book/info/id/".$v["id"],
						);
					}

				}
				$data["articles"] =$noveldata;
				break;
			case '5'://未支付提醒
				
				$data = '';
				$history = db('order')->where(array("uid"=>$uid,"status"=>0))->order("update_time DESC")->find();
				if($history){
					$time = time();
					$activity = db('promotion')->alias('p')->join('cps_pay_setting s','s.id = p.payid')->where(['p.wxmpid'=>$wxmpid,'p.uid'=>$cps_uid,"p.is_diy"=>1,"p.push_nopay"=>1])->where("p.starttime <= $time and p.endtime > $time")->order('p.starttime desc,p.id desc')->field('p.*,s.price,s.egold,s.giveegold,s.type,s.description')->find();
					
					//渠道自定义活动为空则获取默认平台自定义活动
					if(empty($activity)){
						$activity = db('promotion')->alias('p')->join('cps_pay_setting s','s.id = p.payid')->where(['p.wxmpid'=>0,'p.uid'=>1,"p.is_diy"=>1])->where("p.starttime <= $time and p.endtime > $time")->order('p.starttime desc,p.id desc')->field('p.*,s.price,s.egold,s.giveegold,s.type,s.description')->find();
					}
					
					if(!empty($history["articleid"])){
						$articlename = get_novel($history["articleid"],"title");
						$data = "书币订单未支付提醒:".PHP_EOL.PHP_EOL."亲，您有还未完成支付的书币订单，《".$articlename."》后续剧情高'潮迭起，";
					}else{
						$data = "书币订单未支付提醒:".PHP_EOL.PHP_EOL."亲，您有还未完成支付的书币订单.";
					}
					
					
					if($activity){
						$data .= "现充值 ".$activity['price']." 即送".$activity['giveegold']."书币，赶紧行动吧!".PHP_EOL;
						
						$data .= PHP_EOL.'👉<a href="'.$site_url."/diypreferential/".$activity['id'].'">点击充值</a>'.PHP_EOL.PHP_EOL;
					}
					$data .= "小提示：充值包年VIP，每天1元钱，全站书籍均可阅读".PHP_EOL."哦~";

					
				}
				break;
			case '6'://关注图文推送
			case '7'://签到图文推送
				
				$history = db('history')->where(array("model"=>1,"uid"=>$uid))->order("update_time DESC")->find();
				
			    if($history){
					$data[] = array(
						"title"=>"继续阅读：《".$history["name"]."》",
						"picurl"=>$site_url.$cover,
						"url"=>$site_url."/chapter/read/".$history["articleid"]."/".$history["chapterid"],
					);
			    }else{
					$list  = $this->random_data(1,"novel",array("status"=>1,"allvisit"=>array(">=",100)),"id,title,cover");
				
					if($list){

						foreach ($list as $k=>$v){
							$data[] = array(
								"title"=>$v["title"],
								"picurl"=>$site_url.$cover,
								"url"=>$site_url."/book/info/id/".$v["id"],
							);
						}

					}
				}
				
				$time = time();
				$activity = db('promotion')->alias('p')->join('cps_pay_setting s','s.id = p.payid')->where(['p.wxmpid'=>$wxmpid,'p.uid'=>$cps_uid,"p.is_diy"=>1,"p.push_nopay"=>1])->where("p.starttime <= $time and p.endtime > $time")->order('p.starttime desc,p.id desc')->field('p.*,s.price,s.egold,s.giveegold,s.type,s.description')->find();
				//渠道自定义活动为空则获取默认平台自定义活动
				if(empty($activity)){
					$activity = db('promotion')->alias('p')->join('cps_pay_setting s','s.id = p.payid')->where(['p.wxmpid'=>0,'p.uid'=>1,"p.is_diy"=>1])->where("p.starttime <= $time and p.endtime > $time")->order('p.starttime desc,p.id desc')->field('p.*,s.price,s.egold,s.giveegold,s.type,s.description')->find();
				}
				if($activity){
					
					$data[] = array(
						"title"=>"现充值 ".$activity['price']." 即送".$activity['giveegold']."书币，赶紧行动吧!",
						"picurl"=>$site_url.$cover,
						"url"=>$site_url."/diypreferential/".$activity['id'],
					);
				}

				$data["articles"] =$data;
				break;
			case '8'://首充优惠图文推送
				$time = time();
				$data = [];
				$promotion = db('promotion')->alias('p')->join('cps_pay_setting s','s.id = p.payid')->where("p.starttime <= $time and p.endtime > $time and p.template_id=4")->field('p.*,s.price,s.egold,s.giveegold,s.type,s.description')->find();
				if($activity){

					$data[] = array(
						"title"=>"恭喜：您被书币大礼包砸到！\n活动1:现充值 ".$activity['price']." 即送".$activity['giveegold']."书币，赶紧行动吧!",
						"picurl"=>$site_url."/uploads/first-recharge-promotion_1_360x200.jpg",
						"url"=>$site_url."/diypreferential/".$activity['id'],
					);

					//$data .="恭喜：您被书币大礼包砸到！\n";
					/*
					foreach ($info as $k=>$v){
						$data .="活动".($k+1)."：\n<a href='".$site_url."/preferential'>".$v["description"]."</a>\n";
					}*/
					//$data .="↑↑点蓝字，享惊喜福利！\n";
					$data["articles"] =$data;
				}
				break;
		}
		
		return $data;
	}

	public function random_data($num,$table,$where=[],$field=null)
	{
		$pk = Db::name($table)->getPK();//获取主键
		$info = Db::name($table)->where($where)->field($pk)->select();//查询数据
		
		$list = array();
		
		if(count($info)>=$num){
			
			$n = '';
			$qu = '';
			foreach($info as $v=>$val){
				$n[$val[$pk]]= $val[$pk];
			}

			$rand = array_rand($n,$num);
			if(is_array($rand)){
				$qu = implode(',',$rand);
			}else{
				$qu = $rand;
			}

			if($field){
				$list = Db::name($table)->where($pk,'in',$qu)->field($field)->select();
			}else{
				$list = Db::name($table)->where($pk,'in',$qu)->select();
			}
			
		}
		return $list;
	}
}